﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OCR
{
    public partial class Adjust : Form
    {

        private List<Card> results;
        private string folder;
        //是否点击过保存
        private bool isSave = false;
        //是否点击关闭按钮
        private bool isClose = false;
        private ImageView imageView;
        //是否通过
        private bool isPass = false;
        public Adjust(string folder, List<Card> results)
        {
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
            this.folder = folder;
            this.results = results;
         
        }

        private void Adjust_Load(object sender, EventArgs e)
        {
            try
            {
                List<Data> datas = new List<Data>();
                foreach (var result in results)
                {
                    if (result.IsNoDoubt)
                    {
                        Data data = new Data();
                        data.ImageName = result.ImageName;
                        data.CutImage = new Bitmap(Path.Combine(this.folder, data.ImageName + "_NO.jpg"));
                        data.Value = result.NoValue;
                        data.CharValue = result.NoRecognitionValue.CharValue;
                        data.BarCodeValue = result.NoRecognitionValue.BarCodeValue;
                        data.NoPw = NoPw.卡号;
                        data.Folder = this.folder;
                        data.Count = result.Define.NoCount;
                        datas.Add(data);
                    }
                    if (result.IsPwDoubt)
                    {
                        Data data = new Data();
                        data.ImageName = result.ImageName;
                        data.CutImage = new Bitmap(Path.Combine(this.folder, data.ImageName + "_PW.jpg"));
                        data.Value = result.PwValue;
                        data.CharValue = result.PwRecognitionValue.CharValue;
                        data.BarCodeValue = result.PwRecognitionValue.BarCodeValue;
                        data.NoPw = NoPw.密码;
                        data.Folder = this.folder;
                        data.Count = result.Define.PwCount;
                        datas.Add(data);
                    }
                }
                datas.Sort(delegate(Data a, Data b) { return a.NoPw.CompareTo(b.NoPw); });
                this.dataGridView1.DataSource = datas;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
        //原图查看

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == 1 || e.ColumnIndex == 4)
                {
                    string imagePath = this.dataGridView1.Rows[e.RowIndex].Cells["ImageNameFullPath"].Value.ToString();
                    string count = this.dataGridView1.Rows[e.RowIndex].Cells["Count"].Value.ToString();
                    if (this.imageView == null)
                    {
                        this.imageView = new ImageView(imagePath, e.RowIndex, Convert.ToInt32(count));
                        //注册识别事件
                        imageView.OnOkClick += ImageView_OnOkClick;
                        imageView.Show();
                    }
                    else
                    {
                        this.imageView.ImagePath = imagePath;
                        this.imageView.RowIndex = e.RowIndex;
                        this.imageView.Count = Convert.ToInt32(count);
                        this.imageView.ShowImage();
                        this.imageView.Visible = true;
                        this.imageView.TopMost = true;
                    }
                }
            }
        }

        private void ImageView_OnOkClick(object sender, OkEventArgs e)
        {

            this.dataGridView1.Rows[e.RowIndex].Cells["value"].Value = e.Value;
        }


        //调整完成
        private void btnAdjust_Click(object sender, EventArgs e)
        {
            this.isPass = true;
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                string imageName = row.Cells["ImageName"].Value.ToString();
                string value = "";
                if (row.Cells["value"].Value != null)
                {
                    value = row.Cells["value"].Value.ToString().Trim();
                }
                NoPw eNoPw = (NoPw)Enum.Parse(typeof(NoPw), row.Cells["NoPwName"].Value.ToString());
                Card result = this.results.Find(delegate(Card r) { return r.ImageName == imageName; });
                if (eNoPw == NoPw.卡号)
                {
                    result.NoValue = value;
                    if (row.Cells["NoCheck"].Value.ToString() != "True")
                    {
                        if (value.Length != result.Define.NoCount)
                        {
                            row.Cells["value"].Style.BackColor = Color.Red;
                            this.isPass = false;
                        }
                        else
                        {
                            row.Cells["value"].Style.BackColor = Color.White;
                        }
                    }
                    else
                    {
                        row.Cells["value"].Style.BackColor = Color.White;
                    }
                }
                else//密码
                {
                    result.PwValue = value;
                    if (row.Cells["NoCheck"].Value.ToString() != "True")
                    {
                        if (value.Length != result.Define.PwCount)
                        {
                            row.Cells["value"].Style.BackColor = Color.Red;
                            this.isPass = false;
                        }
                        else
                        {
                            row.Cells["value"].Style.BackColor = Color.White;
                        }
                    }
                    else
                    {
                        row.Cells["value"].Style.BackColor = Color.White;
                    }
                }
            }
            isSave = true;
            this.DialogResult = DialogResult.OK;
        }

        private void Adjust_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!this.isClose)
            {
                if (!isSave)
                {
                    MessageBox.Show("先点击保存，再退出");
                    e.Cancel = true;
                }
                else
                {
                    //过滤掉忽略行检查
                    if (!this.isPass)
                    {
                        if (this.results.Exists(d => d.NoValue.Length != d.Define.NoCount || d.PwValue.Length != d.Define.PwCount))
                        {

                            MessageBox.Show("卡号或者密码位数不对!");
                            e.Cancel = true;
                        }
                    }
                }

            }
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.isClose = true;
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }

    public class Data
    {
        //图片名称
        public string ImageName
        {
            get;
            set;
        }
        //截图
        public Image CutImage
        {
            get;
            set;
        }
        //截图值
        public string Value
        {
            get;
            set;
        }
        public string CharValue
        {
            get;
            set;
        }
        public string BarCodeValue
        {
            get;
            set;
        }
        public NoPw NoPw = NoPw.卡号;
        /// <summary>
        /// 位数
        /// </summary>
        public int Count
        {
            get;
            set;
        }
        //图片全路径
        public string Folder
        {
            get;
            set;
        }
        public string NoPwName
        {
            get
            {
                return this.NoPw.ToString();
            }
        }
        public string ImageNameFullPath
        {
            get
            {
                return Path.Combine(this.Folder, this.ImageName + ".jpg");
            }
        }
        private bool noCheck = false;
        public bool NoCheck
        {
            get
            {
                return this.noCheck;
            }
            set
            {
                this.noCheck = value;
            }
        }
    }

}
